<?php
/**
 * Created by PhpStorm.
 * User: huanglinjie
 * Date: 2019/1/9
 * Time: 2:58 PM
 */

namespace app\backstage\controller;
use app\util\ReturnCode;
use app\util\Tools;
use think\Loader;
use app\model\Management as Model;

class Management extends Base {

    public function getList() {
        $page = $this->request->post('page', 1);
        $size = $this->request->post('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $where = [];

        if( isset($this->request->post()['mobile']) ) {
            $where['mobile'] = $this->request->post()['mobile'];
        }
        if(!empty($this->request->post()['trueName']) && $this->request->post()['trueName']){
            $where['trueName'] =['like','%'.$this->request->post()['trueName'].'%'];
        }
        $Model = new Model();
        $List = $Model
            ->limit(($page - 1) * $size, $size)
            ->field('*')
            ->where($where)
            ->select();
        if ($List) {
            $List->append(['statusText']);
        }

        $count = $Model
            ->where($where)
            ->count();//总条数

        return $this->buildSuccess([
            'count'=>$count,
            'list'=>$List
        ]);

    }

    /*
    * 新增后台用户
    * */
    public function add() {
        $postParams = $this->request->post();

        $mobile = Model::get(['mobile' => $postParams['mobile']]);
        if ($mobile){
            return $this->buildFailed(ReturnCode::ADD_FAILED,'该账号已存在，请勿重复添加');
        }
        $result = Model::create([
            'trueName' => $postParams['trueName'],
            'mobile' => $postParams['mobile'],
            'password' => Tools::passwordMd5($postParams['password']),
            'status' =>  isset($postParams['status']) ? $postParams['status'] : config('status.master_status_using'),
            'bankname' => isset($postParams['bankname']) ? $postParams['bankname'] :'',
            'bankaccount' =>isset($postParams['bankaccount']) ? $postParams['bankaccount'] : '',
            'bankdopesit' => isset($postParams['bankdopesit']) ? $postParams['bankdopesit'] :'',
            'lineNumber' => isset($postParams['lineNumber']) ? $postParams['lineNumber'] : '',
        ]);
        if ($result){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(ReturnCode::ADD_FAILED,'添加失败');
        }

    }

    /*
     * 修改后台用户信息
     * */
    public function edit() {
        $postParams = $this->request->post();
        $masterInfo = Model::get(['manageID' => $postParams['manageID']]);
        if (!empty($postParams['mobile']) && $postParams['mobile'] != $masterInfo['mobile']) {
            $mobile = Model::get(['mobile' => $postParams['mobile']]);
            if ($mobile) {
                return Tools::buildReturn(ReturnCode::ADD_FAILED,'该账号已存在');
            }
        }
        $params['trueName'] = $postParams['trueName'] ? $postParams['trueName'] : $masterInfo['trueName'];
        $params['mobile'] = !empty($postParams['mobile']) ? $postParams['mobile'] : $masterInfo['mobile'];
        $params['password'] = !empty($postParams['password']) ? Tools::passwordMd5($postParams['password']) : $masterInfo['password'];;
        $params['status'] =!empty($postParams['status'])  ? $postParams['status'] : $masterInfo['status'];
        $params['bankname'] =!empty($postParams['bankname'])  ? $postParams['bankname'] : $masterInfo['bankname'];
        $params['bankaccount'] =!empty($postParams['bankaccount'])  ? $postParams['bankaccount'] : $masterInfo['bankaccount'];
        $params['bankdopesit'] =!empty($postParams['bankdopesit'])  ? $postParams['bankdopesit'] : $masterInfo['bankdopesit'];
        $params['lineNumber'] =!empty($postParams['lineNumber'])  ? $postParams['lineNumber'] : $masterInfo['lineNumber'];

        $masterWhere['manageID'] = $postParams['manageID'];
        $saveResult = $masterInfo->save($params, $masterWhere);

        if ($saveResult){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(ReturnCode::UPDATE_FAILED,'你没有做出任何修改');
        }
    }

    //禁用
    public function recycle() {
        $where['manageID'] = $this->request->post()['manageID'];
        $data['status'] = $this->request->post()['status'];
        $model = new Model();
        $ret = $model->save($data,$where);
        if ($ret){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(ReturnCode::UPDATE_FAILED,'修改失败');
        }
    }

}